package nl.rug.ai.believer.model;

/**
 * This class represents an agent. Agents only have a name, which is just a label for human users to 
 * easily identify a certain agent.
 * @author S1849700
 *
 */
public class Agent {

	private String name;
	
	/**
	 * Standard constructor, initializes the agent with a name.
	 * @param name the name of the agent
	 */
	public Agent(String name) {
		if (name == null) {
			throw new NullPointerException("Constructor name of Agent was null");
		}
		if (name.indexOf(Util.agentSeparator) != -1 || name.indexOf(Util.agentEndTag) != -1 || 
				name.indexOf(Util.agentStartTag) != -1) {
			throw new RuntimeException("Agent: \"" + name + "\"contains a reserved keyword.");
		}
		this.name = name;
	}
	
	/**
	 * accesor method for the name
	 * @return the name of the agent.
	 */
	public String getName() {
		return name;
	}
	
	
	/**
	 * We consider an Agent equal if their name is equal.
	 * @see Object#equals(Object)
	 */
	@Override 
	public boolean equals(Object other) {
	    //check for self-comparison
	    if ( this == other ) {
	    	return true;
	    }
	    // this renders an explicit check for "other == null" redundant, since it does the check for 
	    // null already - "null instanceof [type]" always returns false. 
	    if (!(other instanceof Agent)) {
	    	return false;
	    }
	    Agent otherAgent = (Agent)other;
	    return otherAgent.name.equals(name);
	}
	
	/**
	 * @see Object#hashCode(Object)
	 */
	// must be overwritten if .equals is overwritten. (And should be based on the same key fields.)
	@Override 
	public int hashCode() {
		return name.hashCode();
	}
}
